#region "ListCollection"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-14 10:23:02
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections
 * 
 * ####     Type Name : ListCollection
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections.ListCollection
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.Collections.Generic;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections.ListCollection</para>
    /// <para>
    /// 重新封装的列表集合。
    /// </para>
    /// </summary>
    /// <typeparam name="T">集合中每个元素的类型。</typeparam>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public class ListCollection<T> : IList<T>
    {
        private List<T> _baseListCollection;
        private bool _readOnly;

        #region BaseListCollection
        /// <summary>
        /// 设置或获取基类中的列表集合对象实例。
        /// </summary>
        protected virtual List<T> BaseListCollection
        {
            get { return _baseListCollection; }
            set { _baseListCollection = value; }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// <para>构造函数：</para>
        /// <para>初始化一个<see cref="ListCollection{T}" />对象实例。</para>
        /// </summary>
        public ListCollection()
        {
            this.BaseListCollection = new List<T>();
            this.ReadOnly = false;
        }

        #endregion

        #region IndexOf
        /// <summary>
        /// 获取<paramref name="element"/>在集合中的索引数字。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型的元素。</param>
        /// <returns>索引数字。</returns>
        public int IndexOf(T element)
        {
            return this.BaseIndexOf(element);
        }

        /// <summary>
        /// 获取<paramref name="element"/>在集合中的索引数字。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型的元素。</param>
        /// <returns>索引数字。</returns>
        protected virtual int BaseIndexOf(T element)
        {
            return this.BaseListCollection.IndexOf(element);
        }
        #endregion

        #region Insert
        /// <summary>
        /// 将<paramref name="element"/>插入到集合中的<paramref name="index"/>位置处。
        /// </summary>
        /// <param name="index">索引位置数字。</param>
        /// <param name="element"><typeparamref name="T"/>类型的元素。</param>
        protected virtual void BaseInsert(int index, T element)
        {
            this.BaseListCollection.Insert(index, element);
        }

        /// <summary>
        /// 将<paramref name="element"/>插入到集合中的<paramref name="index"/>位置处。
        /// </summary>
        /// <param name="index">索引位置数字。</param>
        /// <param name="element"><typeparamref name="T"/>类型的元素。</param>
        public void Insert(int index, T element)
        {
            this.BaseInsert(index, element);
        }
        #endregion

        #region RemoveAt
        /// <summary>
        /// 移除集合中指定索引<paramref name="index"/>处的元素。
        /// </summary>
        /// <param name="index">索引数字。</param>
        protected virtual void BaseRemoveAt(int index)
        {
            this.BaseListCollection.RemoveAt(index);
        }

        /// <summary>
        /// 移除集合中指定索引<paramref name="index"/>处的元素。
        /// </summary>
        /// <param name="index">索引数字。</param>
        public void RemoveAt(int index)
        {
            this.BaseRemoveAt(index);
        }
        #endregion

        #region BaseGet
        /// <summary>
        /// 获取集合中指定索引处的<typeparamref name="T"/>类型元素。
        /// </summary>
        /// <param name="index">索引数字。</param>
        /// <returns><typeparamref name="T"/>类型元素。</returns>
        protected virtual T BaseGet(int index)
        {
            return this.BaseListCollection[index];
        }
        #endregion

        #region BaseSet
        /// <summary>
        /// 设置集合中指定索引处的元素。
        /// </summary>
        /// <param name="index">索引数字。</param>
        /// <param name="element"><typeparamref name="T"/>类型的元素。</param>
        protected virtual void BaseSet(int index, T element)
        {
            this.BaseListCollection[index] = element;
        }
        #endregion

        #region this
        /// <summary>
        /// 设置或获取指定索引处的元素。
        /// </summary>
        /// <param name="index">索引数字。</param>
        /// <returns><typeparamref name="T"/>类型元素。</returns>
        public T this[int index]
        {
            get
            {
                return this.BaseGet(index);
            }
            set
            {
                this.BaseSet(index, value);
            }
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加<paramref name="element"/>到集合中。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        protected virtual void BaseAdd(T element)
        {
            this.BaseListCollection.Add(element);
        }

        /// <summary>
        /// 添加<paramref name="element"/>到集合中。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        public void Add(T element)
        {
            this.BaseAdd(element);
        }
        #endregion

        #region Clear
        /// <summary>
        /// 清空集合。
        /// </summary>
        protected virtual void BaseClear()
        {
            this.BaseListCollection.Clear();
        }

        /// <summary>
        /// 清空集合。
        /// </summary>
        public void Clear()
        {
            this.BaseClear();
        }
        #endregion

        #region Contains
        /// <summary>
        /// 验证集合中是否包含<paramref name="element"/>。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        /// <returns>如果集合中包含<paramref name="element"/>则返回true；否则返回false。</returns>
        protected virtual bool BaseContains(T element)
        {
            return this.BaseListCollection.Contains(element);
        }

        /// <summary>
        /// 验证集合中是否包含<paramref name="element"/>。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        /// <returns>如果集合中包含<paramref name="element"/>则返回true；否则返回false。</returns>
        public bool Contains(T element)
        {
            return this.BaseContains(element);
        }
        #endregion

        #region CopyTo
        /// <summary>
        /// 尚未实现此方法。
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        [Obsolete]
        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Count
        /// <summary>
        /// 获取此集合中元素的个数。
        /// </summary>
        /// <returns>元素的个数。</returns>
        protected virtual int BaseGetCount()
        {
            return this.BaseListCollection.Count;
        }

        /// <summary>
        /// 获取此集合中元素的个数。
        /// </summary>
        public int Count
        {
            get { return this.BaseGetCount(); }
        }
        #endregion

        #region ReadOnly
        /// <summary>
        /// 设置或获取此集合是否只读。
        /// </summary>
        protected virtual bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value; }
        }
        #endregion

        #region IsReadOnly
        /// <summary>
        /// 获取此集合是否只读。
        /// </summary>
        public bool IsReadOnly
        {
            get { return this.ReadOnly; }
        }
        #endregion

        #region Remove
        /// <summary>
        /// 从集合中移除<paramref name="element"/>。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        /// <returns>如果成功移除<paramref name="element"/>则返回true；否则返回false。</returns>
        protected virtual bool BaseRemove(T element)
        {
            return this.BaseListCollection.Remove(element);
        }

        /// <summary>
        /// 从集合中移除<paramref name="element"/>。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        /// <returns>如果成功移除<paramref name="element"/>则返回true；否则返回false。</returns>
        public bool Remove(T element)
        {
            return this.BaseRemove(element);
        }
        #endregion

        #region RemoveMatches
        /// <summary>
        /// 枚举集合元素，并移除匹配的元素。
        /// </summary>
        /// <param name="element"><typeparamref name="T"/>类型元素。</param>
        /// <param name="arguments"><see cref="RemoveMatch{T}"/>委托。</param>
        protected virtual void BaseForeachRemove(T element, params object[] arguments)
        {
            if (arguments[0].ForceTo<RemoveMatch<T>>()(element))
                this.BaseRemove(element);
        }

        /// <summary>
        /// 从集合中移除符合条件的元素。
        /// </summary>
        /// <param name="match"><see cref="RemoveMatch{T}"/>委托。</param>
        public virtual void RemoveMatches(RemoveMatch<T> match)
        {
            DoForeach<T>.Do(new ForEachExpression<T>(this.BaseForeachRemove), this.BaseListCollection.ForceTo<IEnumerable<T>>(), match);
        }
        #endregion

        #region GetEnumerator
        /// <summary>
        /// 获取此集合的<see cref="IEnumerator{T}"/>对象实例。
        /// </summary>
        /// <returns>实现了<see cref="IEnumerator{T}"/>接口的对象实例。</returns>
        public virtual IEnumerator<T> GetEnumerator()
        {
            return this.BaseListCollection.GetEnumerator();
        }
        #endregion

        #region System.Collections.IEnumerable.GetEnumerator
        /// <summary>
        /// 获取集合的<see cref="System.Collections.IEnumerator"/>对象实例。
        /// </summary>
        /// <returns>实现了<see cref="System.Collections.IEnumerator"/>接口的对象实例。</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.BaseListCollection.GetEnumerator();
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion